var tree;
var numberFeature = new Array();
var count = 0;
var inputCount = 0;
var deletenum;
//var numCount = 0;
var checkNumber = new Array();//选中的数值型性状
var xhr  = null;
function init(){
	tree=new dhtmlXTreeObject("treebox","100%","100%",0);
	tree.setImagePath("images/tree/");
	tree.enableCheckBoxes(1);
	tree.enableThreeStateCheckboxes(true);
	tree.setOnCheckHandler(checkClick);
//	tree.loadXML("xml/tree_b.xml");
	var str = document.getElementById("featureTree").value;
//	alert(str);
	tree.loadXMLString(str);
}
/**
 * Ajax检索
 * @param id
 * @param state
 * @return
 */
function retrieval(id, state)
{
	var nums = $( ".numInput");
	for(i = 0 ;i < nums.length; i++){
		if(isNaN(nums[i].value)){
			$( "#dialog" ).dialog( "open" );
			return false;
		}
	}

	var list = tree.getAllChecked(); 

	if(list == null || list==""){
		$( "#choosefirst" ).dialog( "open" );
		return;
	}
		
	var node = list.split(",");
	xhr = null;
	var url = "json/multiChannelRetrieval.html";
	if (node != null && node.length != 0) {
		for ( var i = 0; i < node.length; i++) {
			if (i == 0) {
				url += "?";
			}
			url += "featureId=" + node[i];
			if (i != node.length - 1) {
				url += "&";
			}
		}
	}
	xhr =connectXHR(url,listResult);
}

/**
 * 列出检索结果
 * @return
 */
function listResult(){
	if(xhr == null)return;
	if (xhr.readyState == 4 && xhr.status == 200) {
		var data = eval('(' + xhr.responseText + ')');
		//清除原先的结果列表
		var mytb = document.getElementById("resulttable");
		for(var i = mytb.rows.length - 1;i>=0;i--){
			//alert(mytb.rows.length);
			mytb.deleteRow(i);
			
		}
		if(data != null){
			//填充新的结果列表
			for(var i = 0; i < data.length; i++){
				var resulthtml = '<td><a href="plantManager-plantInfo.html?plantName=' + data[i].plantName + '">' + data[i].plantName + '</a><br/></td>';
				var resulttd = document.createElement("tr");
				resulttd.innerHTML = resulthtml;
				document.getElementById("resulttable").appendChild(resulttd);
			}
		}
	}
}

/**
 * 确定删除输入框
 * @return
 */
function sure(){
	if(deletenum == 0){
		document.getElementById("featureName0").innerHTML = "";
		document.getElementById("number0").value = '';
		checkNumber[deletenum] = 0;
		var hidd = document.getElementById("hidden0");
		if(hidd != null){
			document.getElementById("firstinput").removeChild(hidd);
		}
	}else{
		var nodeD = document.getElementById("inputNum" + deletenum);
		nodeD.parentNode.removeChild(nodeD);
		checkNumber[deletenum] = 0;
	}
}
/**
 * 存储数值型性状Id
 * @param numid
 * @return
 */
function storeNumberId(numid){
	numberFeature[count] = numid;
	count++;
}
/**
 * checkbox点击事件
 * @param id
 * @param state
 * @return
 */
function checkClick(id, state){
	if(state == 0){//uncheck
		for(var i = 0 ; i < checkNumber.length; i++){
			if(checkNumber[i] == id)
				break;
		}
		if(i < checkNumber.length)
			deleteInput(i);
		return;
	}
	//判断是否是数值型特性且没有被选中
	var isNum = false;
	for(var i = 0 ; i < numberFeature.length; i++){
		if(numberFeature[i] == id)
			isNum = true;
	}
	for(var i = 0 ; i < inputCount; i++){
		if(checkNumber[i] == id)
			isNum = false;
	}
	if(isNum){
		if(inputCount == 0 || checkNumber[0] == 0){
			var featureName0 = document.getElementById("featureName0");
			featureName0.innerHTML = id;
			checkNumber[0] = id;
			var hid = document.createElement("input");
			hid.setAttribute("type","hidden" );
			hid.setAttribute("id","hidden0" );
			hid.setAttribute("value",id );
			hid.setAttribute("name","numberFeature" );
			document.getElementById("firstinput").appendChild(hid);
		}else{
			var photohtml = '<div class="inputNum"><label class="featureName" id="featureName' + inputCount + '">' + id
				+ '</label><input type="text" name="number" id="number' + inputCount + '" class="numInput"/><a onclick="deleteInput(' + inputCount +')">'
				+ '<img  src="images/delete3.png"/></a><input type="hidden" name="numberFeature" value="'+id+'"/></div>';
			var html = document.createElement("div");
			html.setAttribute("id","inputNum" + inputCount);
			html.setAttribute("class","inputNum");
			html.innerHTML = photohtml;
	
			document.getElementById("numberInput").appendChild(html);
			checkNumber[inputCount] = id;
		}
		inputCount++;
	}
}

function deleteInput(num){
	$( "#dialog-confirm" ).dialog( "open" );
	deletenum = num;
}
